const app = getApp()
const innerAudioContext = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:{},
    soundState:0,
    steps: []
  },
  //生命周期函数--监听页面加载
  onLoad: function (options) {
    var param;
    app.getParam(options, (rel) => {
      param = rel;
      this.setData({
        id: rel.id
      })
    })
    this.getApi(param.id);
  },
  //获取API
  getApi: function (id) {
    var parms = {
      id:id,
    }
    app.api().Get('api/v1/bestbao/order/reView', parms, (result) => {
      if (result.code == 200) {
        this.setData({
          order: result.data.order,
          steps: result.data.process,
          active: result.data.process.length-1
        });
      }
    })
  },
  //图片预览
  previewImage: function (e) {
    wx.previewImage({
      current: e.target.dataset.src,
        urls: this.data.order.imgs
    })
  },
  //播放语音
  playSound: function (e) {
    let soundState = this.data.soundState;
    switch (soundState) {
      case 1: //暂定播放
        this.setData({
          soundState: 0
        })
        this.stopPlay();
        break;
      default: //重新开始
        this.setData({
          soundState: 1,
          disabled: true
        })
        this.play();
    }
  },
  //播放语音
  play: function (e) {
    if (!app.util().isNull(this.data.order.sound)){
      innerAudioContext.autoplay = true
      innerAudioContext.src = this.data.order.sound,
      innerAudioContext.play()
      innerAudioContext.onEnded((res) => {
        this.setData({
          soundState: 0
        })
        this.stopPlay();
      })
    }
  },
  //暂定播放
  stopPlay: function () {
    innerAudioContext.autoplay = true
    innerAudioContext.src = this.tempSoundPath,
    innerAudioContext.stop(() => {
      console.log('暂定播放')
    })
  },
  //去订阅
  doBuyOrder: function() {
    var param = {
      id: this.data.id,
    }
    app.api().Post("api/v1/bestbao/order/doBuy", param, (rel) => {
      if (200 == rel.code) {
        app.doWechatPay(rel.data, (res) => {
          this.getApi(this.data.id);
        })
      }
    })
  },
  //评价
  onChange: function (event) {
    var param = {
      id: this.data.id,
      rate:event.detail,
    }
    app.api().Post("api/v1/bestbao/order/rate", param, (rel) => {
      if (200 == rel.code) {
        this.getApi(this.data.id);
      }
    })
  }
})